package ua.tyran53red.contacts.user;

import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashMap;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import ua.tyran53red.StartDesktop;

public class UserTab extends JPanel {
	private static final long serialVersionUID = 1L;
	private JTable userTable = null;
	private String userData[][] = null;
	private String userColumb[] = { "" };
	private JScrollPane jsbUser = null;
	private HashMap<Integer, UserObject> userMap = new UserImp().readXML();
	private int counter = 0;

	public UserTab(final int width, final int height) {
		setBackground(new Color(255, 255, 255));
		setLayout(null);
		userData = new String [userMap.size()][1];
		for (UserObject user: userMap.values()) {
			userData[counter][0] = user.getName();
			counter++;
		}
		userTable = new JTable(userData, userColumb);
		userTable.setBackground(new Color(238, 238, 238));
		userTable.setRowHeight(50);
		userTable.setSelectionBackground(new Color(255, 255, 255));
		userTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent arg0) {
				StartDesktop.getInstance("", null).getUserPanel().setUser(userMap.get(userTable.getSelectedRow()).getName(), userMap.get(userTable.getSelectedRow()).getStatus(), null, true);
			}
		});
		jsbUser = new JScrollPane(userTable);
		jsbUser.setBounds(5, 5, width - 15, height - 50);
		add(jsbUser);
	}
	
	public void repaint(int width, int height) {
		jsbUser.setBounds(5, 5, width - 15, height - 100);
		this.repaint();
	}
}
